package com.eistert.java.datastructure._11_SegmentTree;

/**
 * @Description:
 * @Author: ai
 * @create: 2023-04-05 18:23
 */
public class _303_RangeSumQueryImmutable2 {

    class NumArray {
        // sum[i] 存储前i个元素和，sum[0]=0
        // sum[i]存储nums[0...i-1]的和
        private int[] sum;

        public NumArray(int[] nums) {
            sum = new int[nums.length + 1];
            sum[0] = 0;
            for (int i = 1; i < sum.length; i++) {
                sum[i] = sum[i - 1] + nums[i - 1];
            }

        }

        public int sumRange(int left, int right) {
            // sum[right+1] 代表前 right个元素的和
            // sum[left] 代表前 left -1个元素的和
            // right -(left -1) 代表从 left 开始到right的和
            return sum[right + 1] - sum[left];
        }
    }

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(left,right);
 */
}


